package com.demo.utils;

import com.demo.dto.MessageDTo;
import com.demo.observer.AbstractObserver;
import com.demo.service.InternalMsgImpl;
import com.demo.service.MsgService;
import com.demo.service.SMSServiceImpl;
import com.demo.service.WeChatServiceImpl;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author oyz
 */
@Component
public class MsgFactory  {

    public enum Msgtype{
        SMS("sms"),
        WECHAT("wechat"),
        EMAIL("email"),
        INTERNAL_MSG("internalMsg");

        private String type;
        Msgtype(String type){
            this.type = type;
        }

        public String getType(){
            return type;
        }
    }



    public  static final List<AbstractObserver> OBSERVER_LIST = new ArrayList<>(2);

    // 因为是无状态的对象,只是单纯的调用
    protected static final Map<String,MsgService> observers = new HashMap<>(4);


    static {
        observers.put(Msgtype.SMS.getType(),new SMSServiceImpl());
        observers.put(Msgtype.WECHAT.getType(),new WeChatServiceImpl());
        observers.put(Msgtype.INTERNAL_MSG.getType(),new InternalMsgImpl());
    }

    public static MsgService getService(MessageDTo messageDTo){
        return observers.get(messageDTo.getMsgType());

    }
}
